// 版权归 瞄你个汪 所有。使用本代码应遵守相关法律法规和Apache 2.0开源许可要求。
package top.geeke.config;

import cn.hutool.jwt.JWTPayload;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.geeke.config.cache.RedisCache;
import top.geeke.constants.AppCachePool;
import top.geeke.constants.AppConstPool;
import top.geeke.eunm.SysAccountTypeEnum;
import top.geeke.mapper.SysUserMapper;
import top.geeke.mapper.SysUserRoleMapper;
import top.geeke.toolkit.RequestUtils;
import top.geeke.toolkit.SpringBeanManager;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  用户session管理器
 * </p>
 *
 * @author 瞄你个汪
 * @since 2024/6/8 18:59
 */
@Service
public class SysUserSessionManager {
    private final Long EXPIRE_TIME = 30L * 60;
    @Autowired
    private RedisCache redisCache;

    public boolean isSuperAdmin() {
        return getUserSession().getAccountType() == SysAccountTypeEnum.SysSuperAdmin;
    }

    /**
     * 获取当前用户id
     * @return 用户id
     */
    public Long getUserId() {
        HttpServletRequest request = RequestUtils.getRequest();
        if (request == null) return null;

        Object userId = request.getAttribute(JWTPayload.JWT_ID);

        return userId == null ? null : Long.parseLong(userId.toString());
    }

    /**
     * 获取当前用户租户id
     * @return 当前用户租户id
     */
    public Long getTenantId() {
        HttpServletRequest request = RequestUtils.getRequest();
        if (request == null) return null;

        Object tenantId = request.getAttribute(AppConstPool.TENANT_ID);

        return tenantId == null ? null : Long.parseLong(tenantId.toString());
    }

    /**
     * 获取当前用户session信息
     * @return 当前用户session信息
     */
    public SysUserSession getUserSession() {
        return getUserSession(getUserId());
    }

    /**
     * 更新当前用户session信息
     */
    public SysUserSession updateUserSession() {
        return updateUserSession(getUserId());
    }

    /**
     * 获取用户session信息
     * @param userId 用户id
     * @return 用户session信息
     */
    public SysUserSession getUserSession(Long userId) {
        SysUserSession session = redisCache.get(AppCachePool.SYSTEM_USER_SESSION + userId);
        if (session == null) {
            session = updateUserSession(userId);
            redisCache.set(AppCachePool.SYSTEM_USER_SESSION + userId, session, EXPIRE_TIME);
        }
        return session;
    }

    /**
     * 更新用户session信息
     * @param userId 用户id
     */
    public SysUserSession updateUserSession(Long userId) {
        SysUserRoleMapper sysUserRoleMapper = SpringBeanManager.getBean(SysUserRoleMapper.class);
        SysUserMapper sysUserMapper = SpringBeanManager.getBean(SysUserMapper.class);
        SysUserSession session = sysUserMapper.getSysUserSession(userId);
        if (session != null) {
            session.setRoleIds(sysUserRoleMapper.getRoleIds(userId));
            redisCache.set(AppCachePool.SYSTEM_USER_SESSION + userId, session, EXPIRE_TIME);
            return session;
        }
        return null;
    }

    /**
     * <p>
     *  用户Session类
     * </p>
     * @author 瞄你个汪
     * @since 2024/6/8 19:10
     */
    @Data
    public static class SysUserSession implements Serializable {
        private Long id;

        private Long appId;

        private Long tenantId;

        private Long orgId;

        private String orgName;

        private String orgType;

        private Long posId;

        private Long managerId;

        private String account;

        private String realName;

        private SysAccountTypeEnum accountType;

        private List<Long> roleIds;
    }
}
